package com.nptever.webcamera;

import com.nptever.webcamera.impl.request.*;
import com.nptever.webcamera.impl.request.base.SOAPBody;
import com.nptever.webcamera.impl.request.base.SOAPObject;
import com.nptever.webcamera.utils.PTZCommond;
import com.nptever.webcamera.utils.SoapUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * @author ZhangKai
 * @version 1.0
 * @decription IndexService服务
 * @date 2018/11/19 9:44
 */
@Service
public class IndexService {

    private static final Logger logger = LoggerFactory.getLogger(IndexService.class);
    private static final String device_service = "http://192.168.11.70:8080/onvif/device_service";
    private static final String ptz_service = "http://192.168.11.70:8080/onvif/ptz_service";

    /**
     *  获取设备DNS
     */
    public String getDNS() {
        SOAPObject soapObject = new SOAPObject();
        soapObject.setSoapBody(new GetSoapDNSBody());
        try {
            logger.info(SoapUtil.convertBeanToXml(soapObject));
            return SoapUtil.send(device_service, SoapUtil.convertBeanToXml(soapObject));
        } catch (Exception e) {
            logger.error("获取DNS异常");
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取服务地址
     * @return 返回服务地址信息报文
     */
    public String getServices() {
        SOAPObject soapObject = new SOAPObject();

        SOAPBody soapBody = new SOAPBody();
        GetSoapMediaServiceAddressBody getSoapMediaServiceAddressBody = new GetSoapMediaServiceAddressBody();
        soapBody.setGetServices(getSoapMediaServiceAddressBody);

        soapObject.setSoapBody(soapBody);
        try {
            logger.info(SoapUtil.convertBeanToXml(soapObject));
            return SoapUtil.send(device_service, SoapUtil.convertBeanToXml(soapObject));
        } catch (Exception e) {
            logger.error("获取服务地址异常");
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取设备媒体配置信息
     * @return 返回流媒体配置信息报文
     */
    public String getRtsMediaProfiles() {
        SOAPObject soapObject = new SOAPObject();

        SOAPBody soapBody = new SOAPBody();
        GetRtsMediaProfilesBody getRtsMediaProfilesBody = new GetRtsMediaProfilesBody();
        soapBody.setGetServices(getRtsMediaProfilesBody);

        soapObject.setSoapBody(soapBody);
        try {
            logger.info(SoapUtil.convertBeanToXml(soapObject));
            return SoapUtil.send(device_service, SoapUtil.convertBeanToXml(soapObject));
        } catch (Exception e) {
            logger.error("获取媒体配置信息异常");
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取视频编码配置信息
     * @return 返回配置信息报文
     */
    public String getRtsVideoEncoderConfigurationOptions() {
        SOAPObject soapObject = new SOAPObject();

        SOAPBody soapBody = new SOAPBody();
        GetVideoEncoderConfigurationOptionsBody getRtsMediaProfilesBody = new GetVideoEncoderConfigurationOptionsBody();
        soapBody.setGetServices(getRtsMediaProfilesBody);

        soapObject.setSoapBody(soapBody);
        try {
            logger.info(SoapUtil.convertBeanToXml(soapObject));
            return SoapUtil.send(device_service, SoapUtil.convertBeanToXml(soapObject));
        } catch (Exception e) {
            logger.error("获取视频编码配置信息异常");
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 设置视频流配置
     * @return 返回信息报文
     */
    public String setVideoEncoderConfiguration() {

        SOAPObject soapObject = new SOAPObject();

        SOAPBody soapBody = new SOAPBody();
        SetVideoEncoderConfigurationBody setVideoEncoderConfigurationBody = new SetVideoEncoderConfigurationBody();
        VideoConfiguration videoConfiguration = new VideoConfiguration();

        Multicast multicast = new Multicast();
        Address address = new Address();
        multicast.setAddress(address);

        videoConfiguration.setMulticast(multicast);
        videoConfiguration.setH264(new H264());
        videoConfiguration.setRateControl(new RateControl());
        videoConfiguration.setResolution(new Resolution());

        setVideoEncoderConfigurationBody.setConfiguration(videoConfiguration);
        soapBody.setGetServices(setVideoEncoderConfigurationBody);

        soapObject.setSoapBody(soapBody);
        try {
            logger.info(SoapUtil.convertBeanToXml(soapObject));
            return SoapUtil.send(device_service, SoapUtil.convertBeanToXml(soapObject));
        } catch (Exception e) {
            logger.error("设置视频配置异常");
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取设备媒体流地址
     * @return 返回流媒体地址信息报文
     */
    public String getRtsStreamUrl() {

        SOAPObject soapObject = new SOAPObject();

        SOAPBody soapBody = new SOAPBody();
        GetStreamUriBody getStreamUriBody = new GetStreamUriBody();

        StreamSetup streamSetup = new StreamSetup();
        streamSetup.setTransport(new Transport());
        getStreamUriBody.setStreamSetup(streamSetup);

        soapBody.setGetServices(getStreamUriBody);

        soapObject.setSoapBody(soapBody);
        try {
            logger.info(SoapUtil.convertBeanToXml(soapObject));
            return SoapUtil.send(device_service, SoapUtil.convertBeanToXml(soapObject));
        } catch (Exception e) {
            logger.error("获取流媒体地址异常");
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取设备基本信息
     * @return 返回服务响应的设备数据报文
     */
    public String getDeviceInformation() {
        SOAPObject soapObject = new SOAPObject();

        SOAPBody soapBody = new GetDeviceInformationBody();
        soapObject.setSoapBody(soapBody);
        try {
            logger.info(SoapUtil.convertBeanToXml(soapObject));
            return SoapUtil.send(device_service, SoapUtil.convertBeanToXml(soapObject));
        } catch (Exception e) {
            logger.error("获取设备信息异常");
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取设备网关信息
     * @return 返回服务响应的网关数据报文
     */
    public String getNetworkDefaultGateway() {

        SOAPObject soapObject = new SOAPObject();

        SOAPBody soapBody = new GetNetworkDefaultGatewayBody();
        soapObject.setSoapBody(soapBody);
        try {
            logger.info(SoapUtil.convertBeanToXml(soapObject));
            return SoapUtil.send(device_service, SoapUtil.convertBeanToXml(soapObject));
        } catch (Exception e) {
            logger.error("获取网关信息异常");
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取设备网络信息
     * @return 返回服务响应的网络数据报文
     */
    public String getNetworkInterfaces() {
        SOAPObject soapObject = new SOAPObject();

        SOAPBody soapBody = new GetNetworkInterfacesBody();
        soapObject.setSoapBody(soapBody);
        try {
            logger.info(SoapUtil.convertBeanToXml(soapObject));
            return SoapUtil.send(device_service, SoapUtil.convertBeanToXml(soapObject));
        } catch (Exception e) {
            logger.error("获取网络信息异常");
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取设备发现模式
     * @return 返回服务响应的发现模式报文
     */
    public String getDiscoveryMode() {
        SOAPObject soapObject = new SOAPObject();

        SOAPBody soapBody = new GetDiscoveryModeBody();
        soapObject.setSoapBody(soapBody);
        try {
            logger.info(SoapUtil.convertBeanToXml(soapObject));
            return SoapUtil.send(device_service, SoapUtil.convertBeanToXml(soapObject));
        } catch (Exception e) {
            logger.error("获取设备发现模式异常");
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 云台控制
     * @param cmd 控制云台命令 {up, down, left, right, stop, zoomin, zoomout}
     * @return 返回控制信息报文
     */
    public String relativeMove(PTZCommond cmd) {

        SOAPObject soapObject = new SOAPObject();

        SOAPBody soapBody = new SOAPBody();

        // 云台控制请求体
        GetRelativeMoveBody getRelativeMoveBody = new GetRelativeMoveBody();
        Translation translation = new Translation();

        switch (cmd){
            case STOP:
                // 当请求停止时，构建云台停止请求
                soapBody.setGetServices(new GetPTZStopBody());
                soapObject.setSoapBody(soapBody);
                break;
            case MOVE_UP:
            case MOVE_DOWN:
            case MOVE_LEFT:
            case MOVE_RIGHT:
                // 如果是移动相关构造移动控制请求体
                translation.setPanTilt(new PanTilt(cmd));
                getRelativeMoveBody.setTranslation(translation);
                soapBody.setGetServices(getRelativeMoveBody);
                soapObject.setSoapBody(soapBody);
                break;
            case ZOOM_IN:
            case ZOOM_OUT:
                // 如果是zoom相关，构造zoom请求体
                translation.setZoom(new Zoom(cmd));
                getRelativeMoveBody.setTranslation(translation);
                soapBody.setGetServices(getRelativeMoveBody);
                soapObject.setSoapBody(soapBody);
                break;
            default:
                break;
        }

        try {
            logger.info(SoapUtil.convertBeanToXml(soapObject));
            return SoapUtil.send(ptz_service, SoapUtil.convertBeanToXml(soapObject));
        } catch (Exception e) {
            logger.error("云台控制异常");
            e.printStackTrace();
        }
        return null;
    }
}
